package com.umeox.ota.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.os.Environment;
import android.util.TypedValue;
import android.view.WindowManager;

import com.umeox.ota.gloable.App;
import com.umeox.ota.data.remote.NewVersionInfo;
import com.abupdate.iot_libs.OtaAgentPolicy;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.mqtt_libs.connect.ShellUtils;
import com.abupdate.trace.Trace;

import java.io.File;
import java.io.IOException;

/**
 * @author fighter_lee
 * @date 2018/7/16
 */
public class Utils {

    public static Context getApp() {
        return App.sCx;
    }

    public static NewVersionInfo toAidlInfo(VersionInfo versionInfo) {
        NewVersionInfo newVersionInfo = new NewVersionInfo();
        newVersionInfo.content = versionInfo.content;
        newVersionInfo.deltaID = versionInfo.deltaID;
        newVersionInfo.deltaUrl = versionInfo.deltaUrl;
        newVersionInfo.md5sum = versionInfo.md5sum;
        newVersionInfo.oldVersionName = versionInfo.oldVersionName;
        newVersionInfo.versionAlias = versionInfo.versionAlias;
        newVersionInfo.versionName = versionInfo.versionName;
        newVersionInfo.fileSize = versionInfo.fileSize;
        return newVersionInfo;
    }

    /**
     * 获取设备屏幕宽度
     *
     * @return
     */
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) App.sCx.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    /**
     * 获取设备屏幕高度
     *
     * @return
     */
    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) App.sCx.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }


    public static int dp2pxInt(float dp) {
        return (int) dp2px(dp);
    }

    private static float dp2px(float dp) {
        Resources r = Resources.getSystem();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = OtaAgentPolicy.sCx.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static boolean canWrite(String path) {
        File file = new File(path + "/FOTA_TEMP_FILE.temp");
        boolean canWrite = false;
        try {
            canWrite = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (canWrite) {
            file.delete();
        }
        return canWrite;
    }

    /**
     * 重启设备
     */
    public static void reboot() {
        Intent intent = new Intent(Intent.ACTION_REBOOT);
        intent.putExtra("nowait", 1);
        intent.putExtra("interval", 1);
        intent.putExtra("window", 0);
        OtaAgentPolicy.sCx.sendBroadcast(intent);
    }

    public static void moveToBack() {
        Trace.d("iport", "move to back");
        Intent backHome = new Intent(Intent.ACTION_MAIN);
        backHome.addCategory(Intent.CATEGORY_HOME);
        backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        OtaAgentPolicy.sCx.startActivity(backHome);
    }


    public static boolean isSupportAbupdate() {
        ShellUtils.CommandResult commandResult = ShellUtils.execCmd("getprop ro.build.ab_update", false);
        if (commandResult.result == 0) {
            return commandResult.successMsg.contains("true");
        }
        return false;
    }

    public static String setFotaLog(Context context) {

        String path;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //sd卡挂载
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //大于6.0 存放于Android/data/data/包名/cache 此路径不需要申请权限
                path = context.getExternalCacheDir() + "/iport_log.txt";
            } else {
                //小于6.0 存放于内置存储卡根目录
                path = Environment.getExternalStorageDirectory() + "/iport_log.txt";
            }
        } else {
            //sd卡不可用
            path = context.getCacheDir().getAbsolutePath() + File.separator + "iport_log.txt";
        }

        return path;
    }
}
